<template>
  <div class="image-mutil-upload">
    <div class="el-upload-list--picture-card avatar" style="position:relative" v-for="image in imagesList" v-if="image">
      <img :src="image | filePath" class="avatar" />
      <span class="el-upload-list__item-actions" v-if="image">
        <!-- <span class="el-upload-list__item-preview">
          <i class="el-icon-zoom-in" @click="handlePictureCardPreview(image)"></i>
        </span> -->
        <span class="el-upload-list__item-delete">
          <i class="el-icon-delete" @click="handleRemove(index)"></i>
        </span>
      </span>
    </div>
    <div class="upload" v-show="imagesList && imagesList.length < max">

      <el-upload class="avatar-uploader" :action="apiUrl" :show-file-list="false" :on-success="handleAvatarSuccess" :on-error="handleAvatarError"
        :before-upload="beforeAvatarUpload" :headers="headers">
        <!-- <img v-if="imageUrl" :src="imageUrl | filePath" class="avatar" /> -->
        <i class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

    </div>
    <el-dialog :visible.sync="dialogVisible" width="600px">
      <img width="100%" :src="dialogImageUrl | filePath" alt="">
    </el-dialog>
  </div>

</template>
<script>
import { environment } from '../../models/environment';
import { getItem } from '../../script/config/common'
export default {
  name: 'ImageMutilUpload',
  props: {
    image: {
      type: String,
      default: '',
    },
    max: {
      type: Number,
      default: 3,
    },
  },
  data() {
    return {
      imageUrl: '',
      apiUrl: environment.API_URL + '/service/fileUpload',
      headers: {},
      imagesList: this.images || [],
      dialogImageUrl: '',
      dialogVisible: false,
    }
  },
  watch: {
    imagesList(val) {
      this.$emit('update:images', val)
    }
  },
  computed: {

  },
  mounted() {
    let user = getItem('user');
    let token = getItem('token');

    if (user) {
      this.headers.user = user.SysUserId;
      this.headers.purview = (user.PurviewGroupIds || []).join(',');
    }
    if (token) {
      this.headers.authorization = token.token || {};
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      console.log("handleAvatarSuccess -> file", file)
      console.log("handleAvatarSuccess -> res", res)
      // this.imageUrl = res.fname;
      // this.imageUrl = URL.createObjectURL(file.raw);
      if (res.fname) {
        this.imagesList.unshift(res.fname);
      }
    },

    handleAvatarError(err, file, fileList) {
      this.$message.warning('上传失败' || err.message)
    },

    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 5;

      // if (!isJPG) {
      //   this.$message.warning('上传图片只能是 JPG 格式!');
      // }
      if (!isLt2M) {
        this.$message.warning('上传图片大小不能超过 5MB!');
      }
      return isLt2M;
    },

    handleRemove(index) {
      console.log("file: ImageMutilUpload.vue ~ line 94 ~ handleRemove ~ index", index)
      this.imagesList.splice(index, 1);
    },

    handlePictureCardPreview(image) {
      this.dialogImageUrl = image;
      this.dialogVisible = true;
    }
  }
}
</script>

<style lang="scss" scoped>
.image-mutil-upload {
  display: flex;
  flex-wrap: wrap;
  .el-upload-list--picture-card {
    margin-right: 10px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
